//
//  DetailViewOwner.m
//  EcmDoctor
//
//  Created by 张进 on 15/6/4.
//  Copyright (c) 2015年 张进. All rights reserved.
//

#import "QuestionCardView.h"
#import <UIImageView+AFNetworking.h>
#import "NetWorkUtil.h"

@implementation QuestionCardView

@synthesize imageView,topView,leftBottomDate,middleDesc,bqmsStr,picVoiceView,picNum,voiceNum,dWidth,dHeight;

-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if(self)
    {
        //[self.topView setFrame:CGRectMake(0, 0, self.frame.size.width, self.topView.frame.size.height)];
        [self setBackgroundColor:[UIColor colorWithRed:1.0 green:248.0/255 blue:238.0/255 alpha:1.0]];

    }
    return self;
}

+(QuestionCardView *)instanceQuestionCardView
{
    NSArray* nibView =  [[NSBundle mainBundle] loadNibNamed:@"QuestionCardView" owner:nil options:nil];
    return [nibView objectAtIndex:0];
}

//解决 transform 引起的 Constraint 冲突
- (void)layoutSubviews{
    [super layoutSubviews];
//    [self.topView setFrame:CGRectMake(0, 0, self.frame.size.width, self.topView.frame.size.height)];
    [self.topView setFrame:CGRectMake(0, 0, dWidth, self.topView.frame.size.height)];
    NSLog(@"height:%f,width:%f",self.frame.size.height,self.frame.size.width);
    
    self.imageView.layer.cornerRadius = self.imageView.frame.size.width/2;
    self.imageView.clipsToBounds = YES;
    self.imageView.layer.borderWidth = 3.0f;
    self.imageView.layer.borderColor = [UIColor colorWithRed:240.0/255 green:240.0/255 blue:240.0/255 alpha:1.0
                                        ].CGColor;
    
    //底部日期重新布局
    [self.leftBottomDate setFrame:CGRectMake(15, dHeight-self.leftBottomDate.frame.size.height-15, self.leftBottomDate.frame.size.width, self.leftBottomDate.frame.size.height)];
    //右下角图片数量Label
    [self.picVoiceView setFrame:CGRectMake(dWidth-self.picVoiceView.frame.size.width-8, dHeight-self.picVoiceView.frame.size.height-16, self.picVoiceView.frame.size.width, self.picVoiceView.frame.size.height)];
    //垂直居上--病情描述
    [self.middleDesc setFrame:CGRectMake(15, self.bqmsStr.frame.origin.y+self.bqmsStr.frame.size.height+8, dWidth-30, self.leftBottomDate.frame.origin.y-self.bqmsStr.frame.origin.y-self.bqmsStr.frame.size.height-8)];
    
    [self.middleDesc sizeToFit];
//    [self.leftBottomDate ]
    
//    [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTap)]];
    
}

-(void)setOnlineModel:(OnlineModel *)onlineModel {
    CGRect rect = self.picVoiceView.frame;
    
    NSLog(@"%f  %f", rect.origin.x, rect.origin.y);
    
    [self.imageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", [NetWorkUtil getImagesBaseUrl], onlineModel.icon_Url]] placeholderImage:[UIImage imageNamed:@"icon"]];
    
    self.leftBottomDate.text = onlineModel.infodate;
    self.picNum.text = [NSString stringWithFormat:@"%lu", (unsigned long)onlineModel.picUrl.count];
    self.nameLabel.text = onlineModel.name;
    self.ageLabel.text = [onlineModel.age stringValue];
    self.sexLabel.text = onlineModel.sex;
    self.middleDesc.text = onlineModel.content;
    if (onlineModel.voiceUrl != nil && ![onlineModel.voiceUrl isEqualToString:@""]) {
        self.voiceNum.text = @"1";
    } else self.voiceNum.text = @"0";
}

-(void)labelTap{
    UIViewController *controller = [self findViewController:self];
    if (controller) {
//        UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"diagnoseFlow" bundle:nil];
//        UINavigationController *nav2=[secondStroyBoard instantiateViewControllerWithIdentifier:@"nav2"];
//        
//        [controller presentViewController:nav2 animated:YES completion:nil];
        
        [controller.tabBarController performSegueWithIdentifier:@"showQuestionRelyDetail" sender:controller];
        
    }
}


- (UIViewController *)findViewController:(UIView *)sourceView
{
    id target=sourceView;
    while (target) {
        target = ((UIResponder *)target).nextResponder;
        if ([target isKindOfClass:[UIViewController class]]) {
            break;
        }
    }
    return target;
}

@end
